<?php

namespace app\store\controller\auth;

use app\store\controller\BaseController;
use app\store\service\AdminService;
use app\Request;
use think\exception\ValidateException;
use think\facade\View;
use app\admin\model\Admin;

class AdminController extends BaseController
{
    public function index()
    {   
        $keyword = $this->request->param('keyword','');

        $adminService = new AdminService();
        $list = $adminService->getAdminList($keyword);


        return View::fetch('',['list' => $list]);
    }


    public function create()
    {   
        $adminService = new AdminService();
        $options = $adminService->getRoleOption();
        View::engine()->layout(false);
        return View::fetch('',['options' => $options]);
    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            validate('AdminValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        Admin::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }

    public function save(Request $request)
    {
        $param = $request->post();
        
        try {
            validate('AdminValidate')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $adminService = new AdminService();
        $adminService->saveAdmin($param);

        return json(['code' => 1, 'msg' => '添加成功!']);
    }

    public function edit(Request $request)
    {
        $id = $request->get('id/d');

        $adminService = new AdminService();
        $options = $adminService->getRoleOption($id);
        $row = $adminService->findAdmin($id);

        View::engine()->layout(false);
        return View::fetch('',['options' => $options,'row' => $row]);
    }

    public function update(Request $request)
    {
        $param = $request->post();

        try {
            validate('AdminValidate.edit')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $adminService = new AdminService();
        $adminService->updateAdmin($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function delete(Request $request)
    {
        $id = $request->get('id/d');
        $adminService = new AdminService();
         $adminService->deleteAdmin($id);

        return redirect(url('admin/admin/index'));
    }

    public function me()
    {   
        $id = session('user_id');

        $adminService = new AdminService();
        $row = $adminService->findAdmin($id);

        return View::fetch('',['row' => $row]);
    }


    public function change(Request $request)
    {
        $param = $request->post();

        try {
            validate('AdminValidate.me')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $adminService = new AdminService();
        $adminService->changeAdmin($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function logout()
    {
        session('user_id',null);
        session('nickname',null);

        return redirect(url('/admin/login'));
    }

    
}